*****State Data


library("arm")
library(geepack)
library(Hmisc)
library(foreign) 

setwd("/Users/jhp3000/Documents/Dropbox/Late Budgets")

Budgets <- read.dta("filtered_6_8_11_updated.dta")
#Budgets$weeks_after_fy_meet <- Budgets$days_after_fy_meet /7
#Budgets$weeks_after_squared <- Budgets$weeks_after_fy_meet  * Budgets$weeks_after_fy_meet
#Budgets$days_after_squared <- Budgets$days_after_fy_meet *  Budgets$days_after_fy_meet

attach(Budgets)

late.model1 <- glmer(formula = budget_late_update ~ 
      shutdown_new 
    + election_year
    + election_year:shutdown
    + (1|state) + (1|yearword), 
    family=binomial(link="logit"))
    
display(late.model1)


late.model2 <- glmer(formula = budget_late_update ~ 
    rescale(days_after_fy_meet)
    + (1|state) + (1|yearword), 
    family=binomial(link="logit"))
    
display(late.model2)

late.model3 <- glmer(formula = budget_late_update ~ 
   divided_government
   + supermajority_budget
    + (1|state) + (1|yearword), 
    family=binomial(link="logit"))
    
display(late.model3)

late.model4 <- glmer(formula = budget_late_update ~ 
      shutdown_new 
    + election_year
    + election_year:shutdown_new
   + divided_government
   + rescale(days_after_fy_meet)
   + supermajority_budget
   + rescale(income_rate) 
   + budget_biennial 
   + rescale(total_exp_adj)
   + rescale(surplus)
   + rescale(startmon)        
    + (1|state) + (1|yearword), 
    family=binomial(link="logit"))
    
display(late.model4)

late.model5 <- glmer(formula = budget_late_update ~ 
      shutdown_new 
    + election_year
    + election_year:shutdown_new
   + divided_government
   + rescale(days_after_fy_meet)
  + rescale(days_after_fy_meet):shutdown_new                 
   + supermajority_budget
   + rescale(income_rate) 
   + budget_biennial 
   + rescale(total_exp_adj)
   + rescale(surplus)
   + rescale(startmon) 
    + (1|state) + (1|yearword), 
    family=binomial(link="logit"))
    
display(late.model5)


late.model6 <- glmer(formula = budget_late_update ~ 
                      shutdown_new 
                     + election_year
                     + election_year:shutdown_new
                     + divided_government
                     + rescale(days_after_fy_meet)
                     + rescale(days_after_fy_meet):shutdown_new                 
                     + supermajority_budget
                     + rescale(income_rate) 
                     + budget_biennial 
                     + rescale(total_exp_adj)
                     + rescale(surplus)
                     + rescale(startmon) 
                      + rescale(salary_only_share_inc_pc)
                     + (1|state) + (1|yearword), 
                     family=binomial(link="logit"))

display(late.model6)

late.model7 <- glmer(formula = budget_late_update ~ 
      shutdown_new 
    + election_year
    + election_year:shutdown_new
   + divided_government
   + rescale(days_after_fy_meet)
    + + rescale(days_after_fy_meet):shutdown_new 
   + supermajority_budget
   + rescale(income_rate) 
   + budget_biennial 
   + rescale(total_exp_adj)
   + rescale(surplus)
   + rescale(startmon) 
    + rescale(salary_only_share_inc_pc)
    + rescale(abs_taxchanges_all)
    + (1|state) + (1|yearword), 
    family=binomial(link="logit"))
    
display(late.model7)

*************Figure 2


late.model5 <- glmer(formula = budget_late_update ~ 
                      shutdown_new 
                     + election_year
                     + election_year:shutdown_new
                     + divided_government
                     + days_after_fy_meet
                     + days_after_fy_meet:shutdown_new                 
                     + supermajority_budget
                     + rescale(income_rate) 
                     + budget_biennial 
                     + rescale(total_exp_adj)
                     + rescale(surplus)
                     + rescale(startmon) 
                     + (1|state) + (1|yearword), 
                     family=binomial(link="logit"))

display(late.model5)


pdf(file = "test.pdf", height = 8, width = 8)
## set up the new plotting device (pdf)
par(mfrow = c(2,1))
## draw the plot


#shutdown vs not, unified gov

par(mar = c(4, 4, 2.75, 1))
y <- c(1:length(days_between_session_fy_tr))# use for plots
axis.text <- .8
title.size <- .8
#use vertical lines to indicate 1 sd above and below opinion
#sd.below <- mean(days_between_session_fy_tr) - sd(days_between_session_fy_tr)
#sd.above <- mean(days_between_session_fy_tr) + sd(days_between_session_fy_tr)
#sd.width <- .7
shade.color <- "grey80"
x.min <- -150
x.max <- 250
plot((days_after_fy_meet), y, type = "n", xlim = c(x.min,x.max), ylim = c(0,.5), axes = F, xlab = "", ylab = "Pr(Late Budget)"
     , xaxs ="i", yaxs="i")
#polygon(x=c(0, 0, sd.below, sd.below), y=par()$usr[c(3,4,4,3)],col= shade.color, border=F)
#polygon(x=c(sd.above, sd.above, 175,175), y=par()$usr[c(3,4,4,3)],col= shade.color, border=F)
axis(1, at = seq(x.min,x.max,50), mgp = c(2,.7,0), cex.axis = axis.text, label = c("-150", "-100", "-50", "0","50","100", "150", "200", "250"))
axis(2, at = seq(0,.5, by = .10), label = c("0", ".10", ".20", ".30", ".40", ".50"), las =2,  cex.axis = axis.text, mgp = c(2,.5,0))
curve(invlogit(fixef(late.model5)["(Intercept)"] + fixef(late.model5)["days_after_fy_meet"]*x 
               + fixef(late.model5)["shutdown_new"]*(1) + fixef(late.model5)["divided_government"]*(0)
               + fixef(late.model5)["budget_biennial"]*(0)
               + fixef(late.model5)["election_year"]*(0)
               + fixef(late.model5)["supermajority_budget"]*(0)
               + fixef(late.model5)["shutdown_new:election_year"]*(0)
               + fixef(late.model5)["shutdown_new:days_after_fy_meet"]*x
), lwd=2,xlim=c(x.min,x.max), add=TRUE,lty=2) 
curve(invlogit(fixef(late.model5)["(Intercept)"] + fixef(late.model5)["days_after_fy_meet"]*x 
               + fixef(late.model5)["shutdown_new"]*(0) + fixef(late.model5)["divided_government"]*(0)
               + fixef(late.model5)["budget_biennial"]*(0)
               + fixef(late.model5)["election_year"]*(0)
               + fixef(late.model5)["supermajority_budget"]*(0)
               + fixef(late.model5)["shutdown_new:election_year"]*(0)
               + fixef(late.model5)["shutdown_new:days_after_fy_meet"]*(0)
), lwd=2,xlim=c(x.min,x.max), add=TRUE,lty=1) 
mtext("A. Shutdown vs. No Shutdown\n(Unified Government)", 3, line = .5, cex = title.size)
text(53, .11, "No") #(lower numbers move left, lower numbers move down)
text(100,.05, "Yes")
#abline(v = sd.below, col = "dark grey", lwd = sd.width)
#abline(v = sd.above, col = "dark grey", lwd = sd.width)
box()


#**shutdown vs not, divided gov

par(mar = c(4, 4, 2.75, 1))
y <- c(1:length(days_after_fy_meet))# use for plots
axis.text <- .8
title.size <- .8
#use vertical lines to indicate 1 sd above and below opinion
#sd.below <- mean(days_between_session_fy_tr) - sd(days_between_session_fy_tr)
#sd.above <- mean(days_between_session_fy_tr) + sd(days_between_session_fy_tr)
#sd.width <- .7
shade.color <- "grey80"
x.min <- -150
x.max <- 250
plot((days_after_fy_meet), y, type = "n", xlim = c(x.min,x.max), ylim = c(0,.5), axes = F, xlab = "End of Session vs. End of Fiscal Year\n (Calendar Days)", ylab = "Pr(Late Budget)"
     , xaxs ="i", yaxs="i")
#polygon(x=c(0, 0, sd.below, sd.below), y=par()$usr[c(3,4,4,3)],col= shade.color, border=F)
#polygon(x=c(sd.above, sd.above, 175,175), y=par()$usr[c(3,4,4,3)],col= shade.color, border=F)
axis(1, at = seq(x.min,x.max,50), mgp = c(2,.7,0), cex.axis = axis.text, label = c("-150", "-100", "-50", "0","50","100", "150", "200", "250"))
axis(2, at = seq(0,.5, by = .10), label = c("0", ".10", ".20", ".30", ".40", ".50"), las =2,  cex.axis = axis.text, mgp = c(2,.5,0))
curve(invlogit(fixef(late.model5)["(Intercept)"] + fixef(late.model5)["days_after_fy_meet"]*x 
               + fixef(late.model5)["shutdown_new"]*(1) + fixef(late.model5)["divided_government"]*(1)
               + fixef(late.model5)["budget_biennial"]*(0)
               + fixef(late.model5)["election_year"]*(0)
               + fixef(late.model5)["supermajority_budget"]*(0)
               + fixef(late.model5)["shutdown_new:election_year"]*(0)
               + fixef(late.model5)["shutdown_new:days_after_fy_meet"]*x
), lwd=2,xlim=c(x.min,x.max), add=TRUE,lty=2) 
curve(invlogit(fixef(late.model5)["(Intercept)"] + fixef(late.model5)["days_after_fy_meet"]*x 
               + fixef(late.model5)["shutdown_new"]*(0) + fixef(late.model5)["divided_government"]*(1)
               + fixef(late.model5)["budget_biennial"]*(0)
               + fixef(late.model5)["election_year"]*(0)
               + fixef(late.model5)["supermajority_budget"]*(0)
               + fixef(late.model5)["shutdown_new:election_year"]*(0)
               + fixef(late.model5)["shutdown_new:days_after_fy_meet"]*(0)
), lwd=2,xlim=c(x.min,x.max), add=TRUE,lty=1) 
mtext("B. Shutdown vs. No Shutdown\n(Divided Government)", 3, line = .5, cex = title.size)
text(53, .15, "No") #(lower numbers move left, lower numbers move down)
text(100,.06, "Yes")
#abline(v = sd.below, col = "dark grey", lwd = sd.width)
#abline(v = sd.above, col = "dark grey", lwd = sd.width)
box()

dev.off()

**************Federal Data
library("arm")
library(geepack)
library(Hmisc)

library("arm")
library(geepack)
library(Hmisc)
library(foreign)


setwd("/Users/jhp3000/Documents/Dropbox/Late Budgets")

Budgets <- read.dta("all_fed_data_11_9_11.dta")

attach(Budgets)


fed.model1 <- glmer(formula = late ~ 
  + election_year_any
                  #    + divided_gov
                  #    + rescale(session_days)
                  #    + rescale(salary_sh_income)
                  #    + rescale(lag_p_surplus)
                  #    + rescale(ch_personal_income)
                  #    + rescale(billsize)
                  + (1|president) + (1|approp_type), 
                  family=binomial(link="logit"))

display(fed.model1)


fed.model2 <- glmer(formula = late ~ 
  #    + election_year_any
  + divided_gov
                  #   + rescale(session_days)
                  #    + rescale(salary_sh_income)
                  #    + rescale(lag_p_surplus)
                  #    + rescale(ch_personal_income)
                  #    + rescale(billsize)
                  + (1|president) + (1|approp_type), 
                  family=binomial(link="logit"))

display(fed.model2)

fed.model2.5 <- glmer(formula = late ~ 
  #    + election_year_any
  #   + divided_gov
  congress_split
                  + simple_divided 
                  #   + rescale(session_days)
                  #    + rescale(salary_sh_income)
                  #    + rescale(lag_p_surplus)
                  #    + rescale(ch_personal_income)
                  #    + rescale(billsize)
                  + (1|president) + (1|approp_type), 
                  family=binomial(link="logit"))

display(fed.model2.5)


fed.model3 <- glmer(formula = late ~ 
  + election_year_any
                  + divided_gov
                  + rescale(lag_p_surplus)
                  + rescale(income_rate)
                  + rescale(billsize)
                  +after_cba
                  + (1|president) + (1|approp_type), 
                  family=binomial(link="logit"))

display(fed.model3)



fed.model4 <- glmer(formula = late ~ 
  + election_year_any
                  + divided_gov
                  # + rescale(session_days)
                  #   + rescale(salary_sh_income)
                  + rescale(lag_p_surplus)
                  + rescale(income_rate)
                  + rescale(billsize)
                  +after_cba
                  +rescale(abs_polarization_congress)
                  #    +rescale(abs_polarization_congress):divided_gov
                  + (1|president) + (1|approp_type), 
                  family=binomial(link="logit"))

display(fed.model4)

ranef (fed.jop4)
se.ranef (fed.jop4)


